<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title></title>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function(){
				var dhtml = new DHTML();
				$("#insertTypeF").click(function(){
					disableField(["btnInsert"]);
					enableField(["btnFile","addBudgetList"]);
					
					$("#job").val("upload");
        			$("#saveForm").attr("enctype","multipart/form-data");
				});
				$("#insertTypeI").click(function(){
					disableField(["btnFile"]);
					enableField(["btnInsert","addBudgetList"]);
					$("#job").val("save");
        			$("#saveForm").removeAttr("enctype");
				});
				$("#btnInsert").click(function(){
					$("#count").val(parseInt($("#count").val())+parseInt(1));
					var tr = dhtml.createElement(document.getElementById("listBudget"), ["tr"], "");
					var td1 = dhtml.createElement(tr, ["td","align=center"], "");
					var allocateCode = dhtml.createElement(td1, ["input","type=text","name=code","id=code"+$("#count").val(),"maxlength=8"], "");
					allocateCode.onkeypress = function () {
						return numberOnly(event, false);
					}
					allocateCode.onblur = function (){
						checkAllocate(allocateCode);
					}
					dhtml.createElement(td1, ["span","id=#code"+$("#count").val(),"class=red"], "*");
					var td2 = dhtml.createElement(tr, ["td","align=center"], "");
					dhtml.createElement(td2, ["input","type=text","name=sapCode","size=30"], "");
					dhtml.createElement(td2, ["span","id=#sapCode"+$("#count").val(),"class=red"], "*");
					var td3 = dhtml.createElement(tr, ["td","align=center"], "");
					dhtml.createElement(td3, ["input","type=text","name=allocateName","size=50"], "");
					dhtml.createElement(td3, ["span","id=#allocateName"+$("#count").val(),"class=red"], "*");
					var td4 = dhtml.createElement(tr, ["td","align=center"], "");
					dhtml.createElement(td4, ["input","type=hidden","name=actionType","value=I"], "");
					var a = dhtml.createElement(td4, ["a","class=acc67-button"], "");
			        $("a").button({
			            icons: {
			                primary: "ui-icon-closethick"
			            },
	            		text: false
			        });
			        a.onclick= function () {
			        	dhtml.removeElement(tr);
						$("#count").val(parseInt($("#count").val())-parseInt(1));
			        };
			        $("#code"+$("#count").val()).focus();
				});

		        $("#btnInsert").button({
		            icons: {
		                primary: "ui-icon-plusthick"
		            }
		        });
		        $("#addBudgetList").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
		        }).click(function(){
		        	if (validateEmpty()){
		        		var result = true;
		        		var budgetYearForCheck = $("#budgetYearForCheck").val().split(",");
		        		for(var i=0;i<budgetYearForCheck.length;i++){
		        			if($("#budgetYear").val() == budgetYearForCheck[i]){
		        				alert("ปีงบประมาณ " + $("#budgetYear").val() + " มีในระบบแล้ว กรุณาระบุใหม่");
		        				$("#budgetYear").val("");
		        				$("#budgetYear").focus();
		        				result = false;
		        				return false;
		        			}
		        		}
		        		if (($("#count").val() == "" || $("#count").val() == null) && $("#job").val()== "save"){
		        			alert("กรุณาเพิ่มข้อมูลที่ต้องการบันทึก");
	        				result = false;
	        				return false;
		        		}
		        		if(result){
				        	$("#saveForm").submit();
		        		}
		        		//}
		        	}
		        });
		        $("#back").button({
		            icons: {
		                primary: "ui-icon-arrowreturnthick-1-w"
		            }
		        });
		        $("[name=delete]").button({
		            icons: {
		                primary: "ui-icon-closethick"
		            },
            		text: false
		        });
		        if ($("#sizeData").val() == "" || $("#sizeData").val() == null){
					disableField(["btnFile", "btnInsert","addBudgetList"]);
		        }
		        $("#editBudgetList").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
		        }).click(function(){
		        	if (validateEmpty()){
						$("[name=code]").removeAttr("disabled");
						$("#job").val("saveEdit");
						$("#saveForm").removeAttr("enctype");
						$("#saveForm").submit();
		        	}
		        });
			});
			function deleteData(code,budgetYear){
				var dhtml = new DHTML();
				if(confirm('คุณต้องการลบรหัสแจ้งจัดสรรเงินให้คู่สัญญา  ('+code+')?')==true){
					$("#listCode").val($("#listCode").val()+code+",");
					var id = code+"|"+budgetYear;
					dhtml.removeElementById(id);
					$("#count").val(parseInt($("#count").val())-parseInt(1));
				}
			}
			function checkAllocate(object){
				if(object.value != "" && object.value != null){
					var allocateStr = $("#allocateStr").val().split(",");
					for(var i=0;i<allocateStr.length;i++){
						if(object.value == allocateStr[i]){
							alert("รหัสแจ้งจัดสรรเงินให้คู่สัญญานี้มีอยู่แล้วกรุณาระบุใหม่");
							object.value = "";
							return false;
						}
					}
				}
			}
			
		</script>
	</head>
	<body>
		<c:set var="auth" value="${el:getMapValue(menuAuth, 'ACC60101')}"/>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="saveForm" id="saveForm" action="${pageContext.request.contextPath}/acc6" method="post">
		<input type="hidden" name="app" id="app" value="ACC60101"/>
		<input type="hidden" name="job" id="job" value="save"/>
		<input type="hidden" name="sizeData" id="sizeData" value="${sizeData}"/>
		<input type="hidden" name="count" id="count" value="${sizeData}"/>
		<input type="hidden" name="budgetYearForCheck" id="budgetYearForCheck" value="${budgetYearForCheck}"/>
		<input type="hidden" name="allocateStr" id="allocateStr" value="${allocateStr}"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC60101}</b></th>
			</tr>
			<tr>
				<td width="20%" class="bold">ปีงบประมาณ : </td>
				<td>
					<c:if test="${empty listData}">
						<input type="text" name="budgetYear" id="budgetYear" onkeypress="return numberOnly(event, false);" maxlength="4" value="${budgetYear}"/>
						<span id="#budgetYear" class="red">*</span>
					</c:if>
					<c:if test="${!empty listData}">
						${budgetYear}
						<input type="hidden" name="budgetYear" id="budgetYear" value="${budgetYear}"/>
					</c:if>
				</td>
			</tr>
			<tr>
				<c:if test="${empty listData}">
					<td><input type="radio" name="insertType" id="insertTypeF"/>นำเข้าจากไฟล์</td>
				<td><input type="file" id="btnFile" name="xlsFile"/></td>
				</c:if>
			</tr>
			<tr>
				<c:if test="${!empty listData}" var="isChecked"/>
				<td><input type="radio" name="insertType" ${isChecked?'checked':''} id="insertTypeI"/>เพิ่มข้อมูลเอง</td>
				<td><button type="button" id="btnInsert" class="acc67-button">เพิ่มข้อมูล</button></td>
			</tr>
		</table>
		<input type="hidden" name="listCode" id="listCode" value=""/>
		<br/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="center" class="ui-state-default" width="25%"><b>รหัสแจ้งจัดสรรเงินให้คู่สัญญา</b></th>
				<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
					<th align="center" class="ui-state-default" width="15%"><b>รหัส SAP</b></th>
				</c:if>
				<th align="center" class="ui-state-default"><b>ชื่อรหัสแจ้งจัดสรรเงินให้คู่สัญญา</b></th>
				<th align="center" class="ui-state-default" width="3%"><b>ลบ</b></th>
			</tr>
			<c:if test="${!empty listData}">
				<c:forEach var="data" items="${listData}" varStatus="x">
					<tr id="${data.ALLOCATE_CODE}|${budgetYear}">
					<%-- 	<c:if test="${data.ALLOCATE_LEVEL != '' && data.PARENT_CODE != ''}">
							<td align="center"><input type="text" name="code" id="code${x.count}" maxlength="8" disabled="disabled" value="${data.ALLOCATE_CODE}"/></td>
						</c:if>
						<c:if test="${data.ALLOCATE_LEVEL == '' && data.PARENT_CODE == ''}">
							<td align="center"><input type="text" name="code" id="code${x.count}" maxlength="8" onkeypress="return numberOnly(event, false);" value="${data.ALLOCATE_CODE}"/></td>
						</c:if> --%>
						<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
							<td align="center">
								${data.ALLOCATE_CODE}
								<input type="hidden" name="code" id="code${x.count}" value="${data.ALLOCATE_CODE}"/>
							</td>
						</c:if>
						<td align="center"><input type="text" name="sapCode" id="sapCode${x.count}" size="25" value="${data.SAP_CODE}"/></td>
						<td align="center">
							<input type="text" name="allocateName" id="allocateName${x.count}" size="60" value="${data.ALLOCATE_NAME}"/>
							<input type="hidden" name="actionType" value="U">
						</td>
						<c:if test="${data.ALLOCATE_LEVEL != '' && data.PARENT_CODE != ''}">
							<td align="center"><button type="button" class="acc67-button" disabled="disabled" name="delete"></button></td>
						</c:if>
						<c:if test="${data.ALLOCATE_LEVEL == '' && data.PARENT_CODE == ''}">
							<td align="center"><button type="button" class="acc67-button" onclick="deleteData('${data.ALLOCATE_CODE}','${budgetYear}')" name="delete"></button></td>
						</c:if>
					</tr>
				</c:forEach>
			</c:if>
			<tbody id="listBudget">
			</tbody>
			<tr>
				<td align="center" colspan="4">
					<c:if test="${empty listData}">
						<c:if test="${fn:contains(auth, 'I')}">
							<button type="button" name="addBudgetList" id="addBudgetList" class="acc67-button">บันทึกข้อมูล</button>
						</c:if>
					</c:if>
					<c:if test="${!empty listData}">
						<c:if test="${fn:contains(auth, 'U')}">
							<button type="button" name="editBudgetList" id="editBudgetList" class="acc67-button">บันทึกข้อมูล</button>
						</c:if>
					</c:if>
						<button type="button" name="back" id="back" class="acc67-button" onclick="window.location='${pageContext.request.contextPath}/acc6?app=ACC60101&job=init'">ย้อนกลับ</button>
				</td>
			</tr>
		</table>
		<br/>
		</form>
	</body>
</html>